package dearchenpush.good.com.qianfanlive.modules.discover.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dearchenpush.good.com.qianfanlive.R;
import dearchenpush.good.com.qianfanlive.modules.discover.adapter.RecyclerHotAdapter;
import dearchenpush.good.com.qianfanlive.modules.discover.bean.RecyclerHotBean;
import dearchenpush.good.com.qianfanlive.url.UrlConfigs;
import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverHotFragment extends Fragment {


    @BindView(R.id.recyclerView_disHotFragment)
    RecyclerView mRecyclerView;
    @BindView(R.id.linearLayout_disHotFragment_change)
    LinearLayout mChange;
    private List<RecyclerHotBean.MessageBean.AnchorsBean> mData;
    private List<RecyclerHotBean.MessageBean.AnchorsBean> mAnchors;
    private RecyclerHotAdapter mAdapter;
    private GridLayoutManager mManager;
    private int flag = 2;

    public DiscoverHotFragment() {
        // Required empty public constructor
    }

    public static DiscoverHotFragment newInstance() {
        DiscoverHotFragment fragment = new DiscoverHotFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_discover_hot, container, false);
        ButterKnife.bind(this, view);

        initData();
        setData();

        return view;
    }

    private void setData() {
        mRecyclerView.setLayoutManager(mManager);
        mRecyclerView.setAdapter(mAdapter);
        loadData();
    }

    private void initData() {
        mManager = new GridLayoutManager(getContext(), 3, LinearLayoutManager.VERTICAL, false);
        mData = new ArrayList<>();
        mAdapter = new RecyclerHotAdapter(getContext());
    }

    public void loadData() {
        OkHttpUtils.get().url(UrlConfigs.DISCOVER_HOT)
                .build().execute(new StringCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                Gson gson = new Gson();
                mAnchors = gson.fromJson(response, RecyclerHotBean.class).getMessage().getAnchors();
                for (int i = 0; i < 9; i++) {
                    mData.add(mAnchors.get(i));
                }
                mAdapter.addAll(mData);
            }
        });
    }

    @OnClick(R.id.linearLayout_disHotFragment_change)
    public void onClick() {
        if (flag == 1) {
            mData.clear();
            mAdapter.clearAll();
            for (int i = 0; i < 9; i++) {
                mData.add(mAnchors.get(i));
            }
            flag = 2;
            mAdapter.addAll(mData);
        } else if (flag == 2) {
            mData.clear();
            mAdapter.clearAll();
            for (int i = 9; i < 18; i++) {
                mData.add(mAnchors.get(i));
            }
            flag = 3;
            mAdapter.addAll(mData);
        } else if (flag==3){
            mData.clear();
            mAdapter.clearAll();
            for (int i = 18; i < 27; i++) {
                mData.add(mAnchors.get(i));
            }
            flag = 1;
            mAdapter.addAll(mData);
        }
    }
}
